# models.py
from django.db import models
from rest_framework import serializers
import json


class GlbFile(models.Model):
    file = models.FileField(upload_to="glb_files/", null=True, blank=True)
    filePath = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        db_table = "xf_glb_file"
        verbose_name = "glb模型"


class ModelConfig(models.Model):
    # 与GlbFile建立一对一关系
    glb_file = models.OneToOneField(
        GlbFile, on_delete=models.CASCADE, related_name="config"
    )

    # 环境配置
    title = models.CharField(
        max_length=255, null=True, blank=True, verbose_name="模型名称"
    )
    is_lock = models.BooleanField(default=False, verbose_name="模型是否加密")
    export = models.BooleanField(default=False, verbose_name="模型曝光")
    tone_map = models.CharField(
        max_length=50, null=True, blank=True, verbose_name="环境滤镜"
    )
    is_background_img = models.BooleanField(
        default=False, verbose_name="是否是黑色背景"
    )
    is_light = models.BooleanField(default=True, verbose_name="是否打开光照")
    camera_near = models.FloatField(default=0.1, verbose_name="相机近段裁剪")
    camera_far = models.FloatField(default=1000, verbose_name="远端剪切")

    # PC场景配置
    pc_scene_pos = models.JSONField(default=dict, verbose_name="PC场景位置")
    pc_camera_pos = models.JSONField(default=dict, verbose_name="PC相机位置")
    pc_controls_target = models.JSONField(default=dict, verbose_name="PC控制器目标")
    pc_controls_distance = models.FloatField(default=10, verbose_name="PC控制器距离")
    pc_controls_max_distance = models.FloatField(
        default=50, verbose_name="PC控制器最大距离"
    )
    pc_controls_min_distance = models.FloatField(
        default=1, verbose_name="PC控制器最小距离"
    )

    # MB场景配置
    mb_scene_pos = models.JSONField(default=dict, verbose_name="MB场景位置")
    mb_camera_pos = models.JSONField(default=dict, verbose_name="MB相机位置")
    mb_controls_target = models.JSONField(default=dict, verbose_name="MB控制器目标")
    mb_controls_distance = models.FloatField(default=10, verbose_name="MB控制器距离")
    mb_controls_max_distance = models.FloatField(
        default=50, verbose_name="MB控制器最大距离"
    )
    mb_controls_min_distance = models.FloatField(
        default=1, verbose_name="MB控制器最小距离"
    )

    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "xf_model_config"
        verbose_name = "模型配置"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.glb_file.filePath}的配置"


class GlbFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = GlbFile
        fields = "__all__"


class ModelConfigSerializer(serializers.ModelSerializer):
    class Meta:
        model = ModelConfig
        fields = "__all__"
